package vn.com.vega.chacha.v2;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.clientserver.JsonAlbum;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.clientserver.JsonVideo;
import vn.com.vega.music.objects.Artist;
import vn.com.vega.music.utils.ArttistUtil;
import vn.com.vega.music.utils.ArttistUtil.LikeAndUnLikeArtistListener;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.AlbumListAdapter;
import vn.com.vega.music.view.adapter.ArtistInfoAdapter;
import vn.com.vega.music.view.adapter.ArtistInfoAdapter.showAndSelectFillter;
import vn.com.vega.music.view.adapter.MixAdapter;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.VideoListAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingListener;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ArtistDetail extends ActivityBase implements AutoLoadingListener {
	public static final String ARTIST_KEY_NAME = "artist key name";
	private static final int HEADER_INDEX = 1;

	private static final String TAG_MENU_LIKE = "Like this album";
	private static final String TAG_MENU_SHARE = "share this album";

	private ListView mListDetailAtist;
	private ArtistInfoAdapter mArtistInfoAdapter;
	private SongListAdapter mSongListAdapter;
	private VideoListAdapter mVideoListAdapter;
	private AlbumListAdapter mAlbumListAdapter;
	private MixAdapter mixAdapter;
	private Artist mArtist;
	private Appinstant mApp;
	private LoadListInfoForArtist mLoadListInfoForArtist;
	private int currentFillter = ArtistInfoAdapter.SONG_FILLTER;
	private LoadingView mLoadingView;
	private View mHeaderView;
	private TextView mSelectFillter;
	private View mFillterView;
	private TopTaskbarView mTopTaskbarView;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.artist_detail_screen);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_artist_detail);
		
		mListDetailAtist = (ListView) findViewById(R.id.lv_artist_detail);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_artist_detail);
		mHeaderView = findViewById(R.id.layout_header_artist_detail);
		mSelectFillter = (TextView) findViewById(R.id.tv_search_fillter_category);
		mFillterView = findViewById(R.id.tv_type_fillter);
		mFillterView.setOnClickListener(selectFillterClickListener);
		mListDetailAtist.setOnScrollListener(onScrollListener);
		mApp = (Appinstant) getApplication();
		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_artist_detail);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);

		fillterType = new ArrayList<String>();
		fillterType.add(ArtistInfoAdapter.SONG_FILLTER_STRING);
		fillterType.add(ArtistInfoAdapter.VIDEO_FILLTER_STRING);
		fillterType.add(ArtistInfoAdapter.AlBUM_FILLTER_STRING);

		init();
		initTopTaskBar();
	}

	private void initTopTaskBar() {
		mTopTaskbarView.showBackButton();
		mTopTaskbarView.showMainMenu(false);
		mTopTaskbarView.addExtractMenu(R.drawable.bt_menu_like_toptask_menu,
				likeClickListener, TAG_MENU_LIKE);
		mTopTaskbarView.addExtractMenu(R.drawable.bt_toptask_menu_share,
				shareClickListener, TAG_MENU_SHARE);
		
		if (mArtist.isFavorite()) {
			mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE).setSelected(true);
		} else {
			mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE).setSelected(false);
		}
	}

	LikeAndUnLikeArtistListener likeAndUnLikeArtistListener = new LikeAndUnLikeArtistListener() {

		@Override
		public void unlikeSuccess(Artist artist) {
			// TODO Auto-generated method stub
			View menuLike = mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE);
			menuLike.setSelected(false);
			showToast("Xóa ca sỹ " + artist.name
					+ " khỏi danh sách yêu thích thành công");
		}

		@Override
		public void unlikeFail(Artist artist, String errorMsg) {
			// TODO Auto-generated method stub
			View menuLike = mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE);
			menuLike.setSelected(true);
			if (Util.checkStringIsemptyOrNull(errorMsg)) {
				showToast("Xóa ca sỹ " + artist.name
						+ " khỏi danh sách yêu thích thất bại");
			} else {
				showToast(errorMsg);
			}
		}

		@Override
		public void startDoUnlike(Artist artist) {
			// TODO Auto-generated method stub
			showToast("Đang thực hiện hủy yêu thích ca sỹ " + artist.name);
		}

		@Override
		public void startDoLike(Artist artist) {
			// TODO Auto-generated method stub
			showToast("Đang thêm ca sỹ " + artist.name
					+ " vào danh sách yêu thích");
		}

		@Override
		public void likeSuccess(Artist artist) {
			// TODO Auto-generated method stub
			View menuLike = mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE);
			menuLike.setSelected(true);
			showToast("Thêm ca sỹ " + artist.name
					+ " vào danh sách yêu thích thành công");
		}

		@Override
		public void likeFail(Artist artist, String errorMsg) {
			// TODO Auto-generated method stub
			View menuLike = mTopTaskbarView.getExtractMenu(TAG_MENU_LIKE);
			menuLike.setSelected(false);
			if (Util.checkStringIsemptyOrNull(errorMsg)) {
				showToast("Thêm ca sỹ " + artist.name
						+ " vào danh sách yêu thích thất bại");
			} else {
				showToast(errorMsg);
			}
		}
	};

	View.OnClickListener likeClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			// TODO check artist is like or unlike before
			ArttistUtil.likeArtist(ArtistDetail.this, mArtist,
					likeAndUnLikeArtistListener);
		}
	};

	View.OnClickListener shareClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Util.share(mArtist.shareMessage, ArtistDetail.this);
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

	private void init() {
		Intent startIntent = getIntent();
		String artistTranportName = startIntent.getStringExtra(ARTIST_KEY_NAME);
		if (artistTranportName == null) {
			finish();
			return;
		}
		Artist artist = mApp.tranportArtis.remove(artistTranportName);
		if (artist == null) {
			finish();
			return;
		}
		mArtist = artist;
		mArtistInfoAdapter = new ArtistInfoAdapter(mArtist, this,
				showAndSelectFillter);
		setFillter(ArtistInfoAdapter.SONG_FILLTER);
		showListSong();
	}

	View.OnClickListener selectFillterClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showSelectFillter();
		}
	};

	OnScrollListener onScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			if (firstVisibleItem >= HEADER_INDEX) {
				mHeaderView.setVisibility(View.VISIBLE);
			} else {
				mHeaderView.setVisibility(View.GONE);
			}
		}
	};

	public void setFillter(int fillter) {
		mSelectFillter.setText(ArtistInfoAdapter.getFillterName(fillter));
		if (currentFillter != fillter) {
			currentFillter = fillter;
			mArtistInfoAdapter.setFillter(currentFillter);
			switch (currentFillter) {
			case ArtistInfoAdapter.SONG_FILLTER:
				showListSong();
				break;
			case ArtistInfoAdapter.VIDEO_FILLTER:
				showListVideo();
				break;
			case ArtistInfoAdapter.ALBUM_FILLTER:
				showListAlbum();
				break;

			default:
				break;
			}
		}
	}

	private static final String DIALOG_FILLTER_NAME = "Fillter dialog";
	private ArrayList<String> fillterType;

	AdapterView.OnItemClickListener onFillterClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			hideDialogFillter(DIALOG_FILLTER_NAME);
			mSelectFillter.setText(fillterType.get(position));
			if (position == 0) {
				setFillter(ArtistInfoAdapter.SONG_FILLTER);
			} else if (position == 1) {
				setFillter(ArtistInfoAdapter.VIDEO_FILLTER);
			} else {
				setFillter(ArtistInfoAdapter.ALBUM_FILLTER);
			}
		}
	};

	private void showSelectFillter() {
		showFilltter(fillterType, onFillterClickListener, DIALOG_FILLTER_NAME);
	}

	showAndSelectFillter showAndSelectFillter = new showAndSelectFillter() {

		@Override
		public void showFillter() {
			// TODO Auto-generated method stub
			showSelectFillter();
		}

		@Override
		public void selectFillter(int fillter) {
			// TODO Auto-generated method stub

		}
	};

	public void showListSong() {
		if (mArtist.listSongs != null) {
			mSongListAdapter = new SongListAdapter(this, mArtist.listSongs,
					false, ArtistDetail.class.getName());
			List<BaseAdapter> listAdapters = new ArrayList<BaseAdapter>();
			listAdapters.add(mArtistInfoAdapter);
			listAdapters.add(mSongListAdapter);
			if (mSongListAdapter.getCount() > 0) {
				mixAdapter = new MixAdapter(listAdapters);
				mListDetailAtist.setAdapter(new AutoLoadingAdapter(mixAdapter,
						this));
			} else {
				mListDetailAtist.setAdapter(mArtistInfoAdapter);
			}

		} else {
			loadList(ArtistInfoAdapter.SONG_FILLTER);
		}
	}

	public void showListVideo() {
		if (mArtist.listVideos != null) {
			mVideoListAdapter = new VideoListAdapter(this, mArtist.listVideos,
					false);
			List<BaseAdapter> listAdapters = new ArrayList<BaseAdapter>();
			listAdapters.add(mArtistInfoAdapter);
			listAdapters.add(mVideoListAdapter);
			if (mVideoListAdapter.getCount() > 0) {
				mixAdapter = new MixAdapter(listAdapters);
				mListDetailAtist.setAdapter(new AutoLoadingAdapter(mixAdapter,
						this));
			} else {
				mListDetailAtist.setAdapter(mArtistInfoAdapter);
			}
		} else {
			loadList(ArtistInfoAdapter.VIDEO_FILLTER);
		}
	}

	public void showListAlbum() {
		if (mArtist.listAlbums != null) {
			mAlbumListAdapter = new AlbumListAdapter(this, mArtist.listAlbums);
			List<BaseAdapter> listAdapters = new ArrayList<BaseAdapter>();
			listAdapters.add(mArtistInfoAdapter);
			listAdapters.add(mAlbumListAdapter);
			if (mAlbumListAdapter.getCount() > 0) {
				mixAdapter = new MixAdapter(listAdapters);
				mListDetailAtist.setAdapter(new AutoLoadingAdapter(mixAdapter,
						this));
			} else {
				mListDetailAtist.setAdapter(mArtistInfoAdapter);
			}
		} else {
			loadList(ArtistInfoAdapter.ALBUM_FILLTER);
		}
	}

	public void showInfoOnly() {
		mArtistInfoAdapter = new ArtistInfoAdapter(mArtist, this,
				showAndSelectFillter);
		List<BaseAdapter> listAdapters = new ArrayList<BaseAdapter>();
		listAdapters.add(mArtistInfoAdapter);
		mixAdapter = new MixAdapter(listAdapters);
		mListDetailAtist.setAdapter(mixAdapter);
	}

	private void loadList(int fillter) {
		if (mLoadListInfoForArtist != null) {
			mLoadListInfoForArtist.cancel(true);
		}
		mLoadListInfoForArtist = new LoadListInfoForArtist(this, fillter);
		mLoadListInfoForArtist.execute();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	static class LoadListInfoForArtist extends AsyncTask<Void, Void, Void> {
		WeakReference<ArtistDetail> artistDetailReference;
		int fillterType;

		public LoadListInfoForArtist(ArtistDetail detail, int type) {
			artistDetailReference = new WeakReference<ArtistDetail>(detail);
			fillterType = type;
		}

		private boolean checkIsLive() {
			ArtistDetail artistDetail = artistDetailReference.get();
			if (artistDetail == null || artistDetail.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return null;
			}
			ArtistDetail artistDetail = artistDetailReference.get();
			switch (fillterType) {
			case ArtistInfoAdapter.SONG_FILLTER:
				JsonSong jsonSong = JsonSong.loadSongListByArtistId(
						artistDetail.mArtist.id, 0);
				if (jsonSong.isSuccess()) {
					artistDetail.mArtist.listSongs = new ArrayList<Object>();
					artistDetail.mArtist.listSongs.addAll(jsonSong.songs);
				} else {
					// Load error, not reload
					artistDetail.mArtist.listSongs = new ArrayList<Object>();
				}
				break;
			case ArtistInfoAdapter.VIDEO_FILLTER:
				JsonVideo jsonVideo = JsonVideo.loadVideoListByArtist(
						artistDetail.mArtist.id, 0);
				if (jsonVideo.isSuccess()) {
					artistDetail.mArtist.listVideos = new ArrayList<Object>();
					artistDetail.mArtist.listVideos.addAll(jsonVideo.videos);
				} else {
					// Load error, not reload
					artistDetail.mArtist.listVideos = new ArrayList<Object>();
				}
				break;
			case ArtistInfoAdapter.ALBUM_FILLTER:
				JsonAlbum jsonAlbum = JsonAlbum.loadAlbummListByArtist(
						artistDetail.mArtist.id, 0);
				if (jsonAlbum.isSuccess()) {
					artistDetail.mArtist.listAlbums = new ArrayList<Object>();
					artistDetail.mArtist.listAlbums.addAll(jsonAlbum.albums);
				} else {
					// Load error, not reload
					artistDetail.mArtist.listAlbums = new ArrayList<Object>();
				}
				break;

			default:
				break;
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkIsLive()) {
				return;
			}
			ArtistDetail artistDetail = artistDetailReference.get();
			artistDetail.mLoadingView.loadingFinish(true, null);
			switch (fillterType) {
			case ArtistInfoAdapter.SONG_FILLTER:
				artistDetail.showListSong();
				break;
			case ArtistInfoAdapter.VIDEO_FILLTER:
				artistDetail.showListVideo();
				break;
			case ArtistInfoAdapter.ALBUM_FILLTER:
				artistDetail.showListAlbum();
				break;
			default:
				break;
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return;
			}
			ArtistDetail artistDetail = artistDetailReference.get();
			artistDetail.mLoadingView.startLoading();
		}

	}

	@Override
	public Activity getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public List<Object> getMoreData() {
		// TODO Auto-generated method stub
		List<Object> result = new ArrayList<Object>();
		switch (currentFillter) {
		case ArtistInfoAdapter.SONG_FILLTER:
			JsonSong jsonSong = JsonSong.loadSongListByArtistId(mArtist.id,
					mSongListAdapter.getCount());
			if (jsonSong.isSuccess()) {
				result.addAll(jsonSong.songs);
			}
			break;
		case ArtistInfoAdapter.VIDEO_FILLTER:
			JsonVideo jsonVideo = JsonVideo.loadVideoListByArtist(mArtist.id,
					mVideoListAdapter.getCount());
			if (jsonVideo.isSuccess()) {
				result.addAll(jsonVideo.videos);
			}
			break;
		case ArtistInfoAdapter.ALBUM_FILLTER:
			JsonAlbum jsonAlbum = JsonAlbum.loadAlbummListByArtist(mArtist.id,
					mSongListAdapter.getCount());
			if (jsonAlbum.isSuccess()) {
				result.addAll(jsonAlbum.albums);
			}
			break;

		default:
			break;
		}

		return result;
	}
}
